﻿using Coravel.Invocable;
using Guoling.ProjectName.Data;

namespace Guoling.ProjectName.ScheduledService.job
{
    public  class MiddleCoravelJob<T> : IInvocable where T : class
    {
        protected readonly ILogger<T> logger;
        protected readonly IServiceProvider provider;
        public MiddleCoravelJob(ILoggerFactory loggerFactory, IServiceProvider serviceProvider) 
        {
            logger = loggerFactory.CreateLogger<T>();
            provider = serviceProvider;
        }
        public virtual Task Invoke()
        {
            return Task.CompletedTask;
        }
    }
    public class MyJob : MiddleCoravelJob<MyJob>
    {
        public MyJob(ILoggerFactory loggerFactory, IServiceProvider serviceProvider) : base(loggerFactory, serviceProvider)
        {
        }
        public override async Task Invoke() 
        {
            using var db = provider.CreateScope().ServiceProvider.GetRequiredService<ApiDbContext>();

            for (int i = 0; ; i++)
            {
              await  db.Users.AddAsync(new User
                {
                    Name = Guid.NewGuid().ToString(),
                    Tel= Guid.NewGuid().ToString(),
                    Gender=Gender.Male
                });
                await db.SaveChangesAsync();
                if (i > 10)
                    break;
            }
            logger.LogInformation("MyCoravelJob添加数据开始!");
           
        }
    }
}
